
Privacy Preference Center
More information
We can create more complex conditional structures by creating nested conditional statements, which is created by placing conditional statements inside other conditional statements:
if (outer condition) {
if (nested condition) {
Instruction to execute if both conditions are true
}
}
When we implement nested conditional statements, the outer statement is evaluated first. If the outer condition is true, then the inner, nested statement is evaluated.
Let’s create a program that helps us decide what to wear based on the weather:
int temp = 45;
boolean raining = true;
if (temp < 60) {
System.out.println("Wear a jacket!");
if (raining == true) {
System.out.println("Bring your umbrella.");
} else {
System.out.println("Leave your umbrella home.");
}
}
In the code snippet above, our
if-then statement: temp < 60. Since temp has a value of 45, this condition is true; therefore, our program will print Wear a jacket!.
Then, we’ll evaluate the condition of the nested if-then statement: raining == true. This condition is also true, so Bring your umbrella is also printed to the screen.
Note that, if the first condition was false, the nested condition would not be evaluated.
1.
The company offers a temporary deal that, if the consumer uses the coupon "ship50", the company will reduce the express shipping price.
Let’s rewrite the body of else-if statement from the last exercise. Inside the else-if statement, create a nested if-then statement that checks if couponCode equals "ship50".
If the nested condition is true, return the value .85.
If the condition is false, use a nested else statement to return the value 1.75.
The solution should look similar to the example below:
public double calculateShipping() {
if (shipping.equals("Regular")) {
return 0;
} else if (shipping.equals("Express")) {
// Add your code here
if (couponCode.equals("couponCodeValue")) {
// return discounted shipping value
} else {
// return full shipping value
}
} else {
return .50;
}
}
High value item! Shipping Shipping cost: 0.85 Order not ready

These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
All Consent Allowed